package com.jvtd.baseProject.data.db;

import android.util.Log;

import com.jvtd.baseProject.bean.db.User;
import com.jvtd.baseProject.rxjava.JvtdRxSchedulers;

import org.litepal.crud.DataSupport;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;


/**
 * Created by Administrator on 2017/10/16.
 * AppDbHelper -> DbHelper
 */
@Singleton
public class AppDbHelper implements DbHelper
{
  private static final String TAG = "AppDbHelper";

  @Inject
  public AppDbHelper()
  {
  }

  @Override
  public Observable<Boolean> saveCurrentUser(final User user)
  {
    return Observable.fromCallable(user::saveOrUpdate);
  }

  @Override
  public Observable<Integer> clearCurrentUser()
  {
    return Observable.fromCallable(() -> DataSupport.deleteAll(User.class)).compose(JvtdRxSchedulers.applyObservableAsync());
  }

  @Override
  public Observable<User> getCurrentUser()
  {
    return Observable.fromCallable(() -> {
      User user = DataSupport.findFirst(User.class);
      Log.i(TAG, "User :" + user);
      return user;
    });
  }
}
